package com.linkage.educloud.ah.adapter.topic;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.PicItem;
import com.linkage.educloud.ah.view.activity.big_picture.BigPictureActivity;
import com.zzhou.common.library.util.ImageCompress;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mr'Tang on 2018/12/6.
 */

public class TopicChooseImgAdapter extends BaseAdapter {

    private Context context;
    private List<Uri> mList = new ArrayList<>();

    public TopicChooseImgAdapter(Context context,List<Uri> list) {
        this.context = context;
        this.mList = list;
    }

    public void addData(List<Uri> list, boolean append) {
        if (list != null && list.size() > 0) {
            if (!append)
                mList.clear();
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void removeData(int position) {
        if (position == mList.size()) {
            return;
        } else {
            mList.remove(position);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        if (mList.size() < 6) {
            return mList.size();
        } else {
            return 6;
        }
    }

    @Override
    public Uri getItem(int arg0) {
        return mList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Holder holder;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_topic_writer_chooseimg, parent, false);
            holder = new Holder();
            holder.item_topic_writer_img = convertView.findViewById(R.id.item_topic_writer_img);
            holder.item_topic_writer_delete = convertView.findViewById(R.id.item_topic_writer_delete);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                String img_path = ImageCompress.getRealPathFromUri(context, mList.get(position));
                Bitmap bm = ImageCompress.getBitmapFromFile(img_path);
                holder.item_topic_writer_img.setImageBitmap(bm);
            }
        });
        holder.item_topic_writer_delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                removeData(position);
            }
        });
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<PicItem> picItems = new ArrayList<>();
                for (int a = 0; a < mList.size(); a++) {
                    PicItem picItem = new PicItem(ImageCompress.getRealPathFromUri(context, mList.get(a)), a);
                    picItems.add(picItem);
                }
                BigPictureActivity.launch(context, picItems, position);
            }
        });
        return convertView;
    }

    class Holder {
        ImageView item_topic_writer_img;
        ImageView item_topic_writer_delete;
    }
}
